/**
 * Created by liubin on 2015/9/27.
 */
'use strict';

import { List } from 'immutable';
import React from 'react';
import { Link } from 'react-router';
import ReactMixin from 'react-mixin';
import { initialize } from 'redux-form';
import Autobind from 'tools/autobindMixin';
import UserAddForm from 'forms/userAddForm';
/**
 * 用户页面
 */
@ReactMixin.decorate(Autobind)
class UserAdd extends React.Component {
    static propTypes = {
        userActions: React.PropTypes.object,
        roleActions: React.PropTypes.object,
        history: React.PropTypes.object.isRequired
    }
    constructor(props) {
        super(props);
        // 初始化state
        this.state = {
            roleCombo: List()
        };
    }
    submitCallback() {
        console.log('submitCallback');
        console.log(this);
        const { history } = this.props;
        history.pushState(null, '/user/list');
    }
    componentDidMount() {
        // 加载combo
        const { roleActions: { selectCombo } } = this.props;
        selectCombo(this.initForm);
    }
    initForm() {
        const { roleStore, dispatch } = this.props;
        const comboList = roleStore.get('comboList');
        const initialValues = {};
        if (comboList.size > 0) {
            const role = comboList.first();
            initialValues['roleId'] = role.get('id');
        }
        dispatch(initialize('userAdd', initialValues));
    }
    //componentWillReceiveProps(nextProps) {
    //    const { roleStore, dispatch } = nextProps;
    //    //roleStore = roleStore.toJS();
    //    const comboList = roleStore.get('comboList');
    //    this.setState({
    //        roleCombo: comboList
    //    }, () => {
    //        console.log(dispatch);
    //        const initialValues = {};
    //        const { roleCombo } = this.state;
    //        if (roleCombo.size > 0) {
    //            const role = roleCombo.first();
    //            console.log(role);
    //            initialValues['roleId'] = role.id;
    //        }
    //        dispatch(initialize('userAdd', initialValues));
    //    });

    //}
    handleSubmit(data) {
        console.log(data);
        const { userActions: { addUser } } = this.props;
        addUser(data, this.submitCallback);
    }
    handleBack() {
        const { history } = this.props;
        const r = history.goBack();
        console.log(r);
    }
    render(){
        //const { roleCombo } = this.state;
        const { roleStore } = this.props;
        const props = {
            roleStore,
            onSubmit: this.handleSubmit,
            handleBack: this.handleBack
        };
        return (
           <Grid>
                <Row>
                    <Col sm={12}>
                        <Breadcrumb>
                            <BLink><Link to='/'>主页</Link></BLink>
                            <BLink><Link to='/user'>用户</Link></BLink>
                            <BLink active>用户新增</BLink>
                        </Breadcrumb>
                    </Col>
                </Row>
               <Row>
                   <Col sm={12} md={8} lg={6}>
                       <PanelContainer plain className='plain-panel' controlStyles='bg-blue fg-white'>
                            <PanelHeader className='bg-blue fg-white'>
                                <Grid>
                                    <Row>
                                        <Col xs={12}>
                                            <h4>用户新增</h4>
                                        </Col>
                                    </Row>
                                </Grid>
                            </PanelHeader>
                            <PanelBody style={{paddingBottom: 10}}>
                                <Grid>
                                    <Row>
                                        <Col xs={12}>
                                            <UserAddForm {...props}/>
                                        </Col>
                                    </Row>
                                </Grid>
                            </PanelBody>
                        </PanelContainer>
                   </Col>
               </Row>
           </Grid>
        );
    }
}
export default UserAdd;